package com.gtpickupapp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SearchSport extends Activity {
	final List<String> spinnerValue = new ArrayList<String>();
	private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    private TextView mEndDateDisplay;
    private Button mEndPickDate;
    private int mEndYear;
    private int mEndMonth;
    private int mEndDay;
    
    static final int DATE_DIALOG_ID = 0;
    static final int END_DATE_DIALOG_ID = 1;
    
   
    
    
    @Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);        
		 View v = LayoutInflater.from(this.getParent()).inflate(R.layout.searchsport, null);
		 setContentView(v);
		 
		 mDateDisplay = (TextView) findViewById(R.id.sport_start_year);
		 mPickDate = (Button) findViewById(R.id.sport_start_date);
		 mPickDate.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));

		 mEndDateDisplay = (TextView) findViewById(R.id.sport_end_year);
		 mEndPickDate = (Button) findViewById(R.id.sport_end_date);
		 mEndPickDate.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
	        // add a click listener to the button
	        mPickDate.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                showDialog(DATE_DIALOG_ID);
	            }
	        });
		 
	        mEndPickDate.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                showDialog(END_DATE_DIALOG_ID);
	            }
	        });
		 
	        // get the current date
	        final Calendar c = Calendar.getInstance();
	        mYear = c.get(Calendar.YEAR);
	        mMonth = c.get(Calendar.MONTH);
	        mDay = c.get(Calendar.DAY_OF_MONTH);
	        
	        mEndYear = c.get(Calendar.YEAR);
	        mEndMonth = c.get(Calendar.MONTH);
	        mEndDay = c.get(Calendar.DAY_OF_MONTH);

	        // display the current date (this method is below)
	        updateDisplay();
	        updateEndDisplay();
	        
	        
	        //weight
			Spinner spinner = (Spinner) findViewById(R.id.sporttype);
			spinner.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
		    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		         this , R.array.sport_array, android.R.layout.simple_spinner_item);
		    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    spinner.setAdapter(adapter);
		    spinnerValue.add(spinner.getSelectedItem().toString());
		    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	        
	        Button search = (Button) findViewById(R.id.sport_search_for_events);
	        
			search.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
			search.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                
	            	String sport = spinnerValue.get(spinnerValue.size() - 1) ;
	            	
	            	Bundle bundle = getIntent().getExtras();	
	            	int realStartMonth = mMonth + 1;
	            	String start = mYear + "-"+ realStartMonth + "-" + mDay;
	            	int realEndMonth = mEndMonth + 1;
	            	String end = mEndYear + "-" + realEndMonth + "-" + mEndDay;
	            	bundle.putString("start", start);
	            	bundle.putString("end", end);
	            	bundle.putString("sport", sport);
		        	Intent myintent = new Intent(SearchSport.this, SportResults.class);
		        	myintent.putExtras(bundle);
		        	SearchEventsGroup g = SearchEventsGroup.group;
		        	LocalActivityManager la = g.getLocalActivityManager();
		        	View view = la.startActivity("sportresults",myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
		        	SearchEventsGroup.group.replaceView(view);
	            }
	        });
		
	}
    
    
    public class MyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
           spinnerValue.add(parent.getItemAtPosition(pos).toString());    
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
    
	
	 private DatePickerDialog.OnDateSetListener mDateSetListener =
         new DatePickerDialog.OnDateSetListener() {

             public void onDateSet(DatePicker view, int year, 
                                   int monthOfYear, int dayOfMonth) {
                 mYear = year;
                 mMonth = monthOfYear;
                 mDay = dayOfMonth;
                 updateDisplay();
             }
         };

    	 private DatePickerDialog.OnDateSetListener mEndDateSetListener =
             new DatePickerDialog.OnDateSetListener() {

                 public void onDateSet(DatePicker view, int year, 
                                       int monthOfYear, int dayOfMonth) {
                     mEndYear = year;
                     mEndMonth = monthOfYear;
                     mEndDay = dayOfMonth;
                     updateEndDisplay();
                 }
             };
         
         
	 @Override  
	 public void onBackPressed() {  
		 SearchEventsGroup.group.back();  
	     return;  
	 }
	 
	 // updates the date in the TextView
	    private void updateDisplay() {
	        mDateDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mMonth + 1).append("-")
	                    .append(mDay).append("-")
	                    .append(mYear).append(" "));
	    }
	    
	    private void updateEndDisplay() {
	        mEndDateDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mEndMonth + 1).append("-")
	                    .append(mEndDay).append("-")
	                    .append(mEndYear).append(" "));
	    }
	    
	    @Override
	    protected Dialog onCreateDialog(int id) {
	        switch (id) {
	        case DATE_DIALOG_ID:
	            return new DatePickerDialog(getParent(),
	                        mDateSetListener,
	                        mYear, mMonth, mDay);
	        case END_DATE_DIALOG_ID:
	        	return new DatePickerDialog(getParent(),
                        mEndDateSetListener,
                        mEndYear, mEndMonth, mEndDay);
	        }
	        return null;
	    }
	
	
}
